package com.hbw.pinyin;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hbw.pinyin.db.DBFactory;
import com.hbw.pinyin.db.DBRecord;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AppCompatEditText content;
    private AppCompatButton save;
    private RecyclerView recyclerView;
    private TextView pinyin;
    private String key;
    private String value;
    private List<DBRecord> list = new ArrayList<>();
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = findViewById(R.id.content);
        save = findViewById(R.id.save);
        recyclerView = findViewById(R.id.recycler_view);
        pinyin = findViewById(R.id.pinyin);

        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                key = editable.toString().trim();
                value = toHanyuPinyinStringArray(key);
                pinyin.setText(value);
                pinyin.setVisibility(View.VISIBLE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DBRecord dbRecord = new DBRecord();
                dbRecord.setKey(key);
                dbRecord.setValue(value);
                dbRecord.setCreate_time(getStringDate());
                DBFactory.sharedSessions().getDBRecordDao().save(dbRecord);
                updateList();
                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            }
        });

        myAdapter = new MyAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdapter);
        updateList();
    }

    private void updateList() {
        list.clear();
        list.addAll(DBFactory.sharedSessions().getDBRecordDao().loadAll());
        myAdapter.notifyDataSetChanged();
    }

    private String toHanyuPinyinStringArray(String str) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        char[] chars = str.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (char mChar : chars) {
            try {
                String[] cStrFormatHY = PinyinHelper.toHanyuPinyinStringArray(mChar, format);
                if (cStrFormatHY.length > 0) {
                    sb.append(cStrFormatHY[0].substring(0, 1));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.main_item_layout, parent, false));
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DBRecord dbRecord = (DBRecord) view.getTag();
                    DBFactory.sharedSessions().getDBRecordDao().delete(dbRecord);
                    updateList();
                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(MainActivity.MyViewHolder holder, int position) {
            DBRecord dbRecord = list.get(position);
            holder.key.setText((position + 1) + ". " + dbRecord.getKey());
            holder.value.setText(dbRecord.getValue());
            holder.delete.setTag(dbRecord);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView key;
        TextView value;
        TextView delete;

        public MyViewHolder(View view) {
            super(view);
            key = view.findViewById(R.id.key);
            value = view.findViewById(R.id.value);
            delete = view.findViewById(R.id.delete);
        }
    }
}